package com.si3.controlador;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.si3.modelo.Alumno;
import com.si3.modelo.Centro;
import com.si3.modelo.Departamento;
import com.si3.modelo.Login;
import com.si3.modelo.Profesor;
import com.si3.modelo.Usuario;
import com.si3.servicio.LoginService;

 
public class LoginValidator implements Validator 
{
	
  private LoginService loginService;

  public void setLoginService(LoginService service)
  {
    this.loginService = service;
  }

  public LoginService getLoginService()
  {
    return loginService;
  }
  
  public boolean supports(Class type) {
    return Login.class.isAssignableFrom(type);
  }
  
  public void validate(Object command, Errors errors)
  {
    Login login = (Login) command;
    
    
     
   ValidationUtils.rejectIfEmptyOrWhitespace(errors, "user", "error.login.user", "Usuario no introducido");
   ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.login.password", "Introduzca password");

   this.validateLogin(login, errors);
  }

//El apellido solo puede tener letras 
private void    validateLogin(Login login, Errors errors)
{
	if (login.getUser().trim()!="")
	{
	  login = loginService.check(login);
	
	  if (login==null)
	  {
	    errors.rejectValue("user","error.login.user.nomatches", 
        "Usuario no encontrado"); 
	  } 
	}
}
  
}

